package com.atgm.mall.gmmallperimall.service.impl;

import com.atgm.common.constant.PeriMallConstant;
import com.atgm.common.utils.GmMallUtils;
import com.atgm.mall.gmmallperimall.dao.*;
import com.atgm.mall.gmmallperimall.entity.*;
import com.atgm.mall.gmmallperimall.service.PeriGoodsAttrService;
import com.atgm.mall.gmmallperimall.vo.resp.PeriGoodsAttrRealVo;
import com.atgm.mall.gmmallperimall.vo.resp.PeriGoodsAttrVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class PeriGoodsAttrServiceImpl extends ServiceImpl<PeriGoodsAttrDao, PeriGoodsAttrEntity> implements PeriGoodsAttrService {

    @Autowired
    private PeriGoodsDao periGoodsDao;

    @Autowired
    private PeriGoodsAttrGroupDao periGoodsAttrGroupDao;

    @Autowired
    private PeriGoodsAttrAttrGroupRelaDao periGoodsAttrAttrGroupRelaDao;

    @Autowired
    private PeriGoodsAttrRelaDao periGoodsAttrRelaDao;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public List<PeriGoodsAttrVo> queryPeriGoodsAttrInfoByPeriGoodsId(Long periGoodsId) {
        //返回值
        List<PeriGoodsAttrVo> periGoodsAttrVoList = new ArrayList<>();
        //获取该周边商品的分类id
        QueryWrapper<PeriGoodsEntity> periGoodsEntityQueryWrapper = new QueryWrapper<>();
        periGoodsEntityQueryWrapper.select("cat_id");
        periGoodsEntityQueryWrapper.eq("id", periGoodsId);
        PeriGoodsEntity periGoodsEntity = periGoodsDao.selectOne(periGoodsEntityQueryWrapper);
        if (periGoodsEntity == null) {
            return null;
        }
        Long catId = periGoodsEntity.getCatId(); //周边商品分类id

        //获取该周边商品分类id下的属性分组
        QueryWrapper<PeriGoodsAttrGroupEntity> periGoodsAttrGroupEntityQueryWrapper = new QueryWrapper<>();
        periGoodsAttrGroupEntityQueryWrapper.eq("cat_id", catId);
        List<PeriGoodsAttrGroupEntity> periGoodsAttrGroupEntities = periGoodsAttrGroupDao.selectList(periGoodsAttrGroupEntityQueryWrapper);

        periGoodsAttrGroupEntities.forEach(periGoodsAttrGroupEntity -> {
            PeriGoodsAttrVo periGoodsAttrVo = new PeriGoodsAttrVo();
            periGoodsAttrVo.setAttrGroupId(periGoodsAttrGroupEntity.getAttrGroupId());
            periGoodsAttrVo.setAttGroupName(periGoodsAttrGroupEntity.getAttrGroupName());
            //获取属性分组下的属性组
            //填充属性值信息
            List<PeriGoodsAttrRealVo> periGoodsAttrRealVoList = new ArrayList<>();

            QueryWrapper<PeriGoodsAttrAttrGroupRelaEntity> periGoodsAttrAttrGroupRelaEntityQueryWrapper = new QueryWrapper<>();
            periGoodsAttrAttrGroupRelaEntityQueryWrapper.eq("attr_group_id", periGoodsAttrGroupEntity.getAttrGroupId());
            List<PeriGoodsAttrAttrGroupRelaEntity> periGoodsAttrAttrGroupRelaEntities = periGoodsAttrAttrGroupRelaDao.selectList(periGoodsAttrAttrGroupRelaEntityQueryWrapper);
            //获取属性值信息
            if (GmMallUtils.isNotEmpty(periGoodsAttrAttrGroupRelaEntities)) {
                List<Long> attrIdList = periGoodsAttrAttrGroupRelaEntities.stream().map(PeriGoodsAttrAttrGroupRelaEntity::getAttrId).collect(Collectors.toList());
                QueryWrapper<PeriGoodsAttrEntity> periGoodsAttrEntityQueryWrapper = new QueryWrapper<>();
                periGoodsAttrEntityQueryWrapper.in("attr_id", attrIdList);
                List<PeriGoodsAttrEntity> periGoodsAttrEntities = baseMapper.selectList(periGoodsAttrEntityQueryWrapper);
                //获取属性值、属性value
                if (GmMallUtils.isNotEmpty(periGoodsAttrEntities)) {
                    attrIdList = periGoodsAttrEntities.stream().map(PeriGoodsAttrEntity::getAttrId).collect(Collectors.toList());
                    QueryWrapper<PeriGoodsAttrRelaEntity> periGoodsAttrRelaEntityQueryWrapper = new QueryWrapper<>();
                    periGoodsAttrRelaEntityQueryWrapper.in("attr_id", attrIdList);
                    periGoodsAttrRelaEntityQueryWrapper.eq("peri_goods_id", periGoodsId);
                    List<PeriGoodsAttrRelaEntity> periGoodsAttrRelaEntities = periGoodsAttrRelaDao.selectList(periGoodsAttrRelaEntityQueryWrapper);
                    periGoodsAttrRelaEntities.forEach(periGoodsAttrRelaEntity -> {
                        PeriGoodsAttrRealVo periGoodsAttrRealVo = new PeriGoodsAttrRealVo();
                        periGoodsAttrRealVo.setAttrId(periGoodsAttrRelaEntity.getAttrId());
                        periGoodsAttrRealVo.setAttrName(periGoodsAttrEntities.stream().filter(periGoodsAttrEntity -> {
                            return periGoodsAttrEntity.getAttrId().equals(periGoodsAttrRelaEntity.getAttrId());
                        }).findAny().get().getAttrName());
                        periGoodsAttrRealVo.setAttrValue(periGoodsAttrRelaEntity.getAttrValue());
                        periGoodsAttrRealVoList.add(periGoodsAttrRealVo);
                    });
                }
            }
            periGoodsAttrVo.setAttrList(periGoodsAttrRealVoList);
            periGoodsAttrVoList.add(periGoodsAttrVo);
        });

        //获取售后保障信息
        String afterSaleGuarantee = stringRedisTemplate.opsForValue().get(PeriMallConstant.AFTER_SALE_GUARANTEE + catId);
        PeriGoodsAttrVo periGoodsAttrVo = new PeriGoodsAttrVo();
        periGoodsAttrVo.setAttGroupName("售后保障");
        periGoodsAttrVo.setAttrGroupValue(afterSaleGuarantee);
        periGoodsAttrVoList.add(periGoodsAttrVo);

        return periGoodsAttrVoList;
    }
}
